//
//  BSLPush.m
//  Chamleon-template
//
//  Created by YLW on 15/10/19.
//
//

#import "BSLPush.h"
#import <MainModuleSDK/DataCenter.h>

@implementation CDVPushInfo

- (instancetype)init
{
    if (self = [super init])
    {
        self.notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        self.fireTimeInterval = 5;
        self.alertBody = @"欢迎使用变色龙推送功能";
        self.alertAction = @"欢迎使用变色龙推送功能（滑动解锁）";
        self.hasAction = YES;
        self.alertLaunchImage = nil;
        self.alertTitle = @"变色龙工具";
        self.userInfo = nil;
    }
    return self;
}


@end

@implementation BSLPush



- (void)addEventListener:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    if (self.pushInfo.isRegisterNotification)return;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReceivedMessagePush) name:@"ReceivedMessagePush" object:nil];
    self.pushInfo.isRegisterNotification = YES;
    
}

- (void)ReceivedMessagePush
{
    NSString *JSStr = [NSString stringWithFormat:@"bsl.push.triggerListener(\'%@\')",@"click"];
    UIWebView *web = self.webView;
    [web stringByEvaluatingJavaScriptFromString:JSStr];
}

- (void)createMessage:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    
    
    NSString *content = [command argumentAtIndex:0 withDefault:nil];
    
    if (content)
    {
        self.pushInfo.alertBody = content;
    }
    
//    int payload = [[command argumentAtIndex:1 withDefault:nil] intValue];
    
    NSDictionary *options = [command argumentAtIndex:2 withDefault:nil];
    
    NSString *title = options[@"title"];
    NSString *sound = options[@"sound"];
    NSTimeInterval delay = [options[@"delay"] floatValue];
    
    if (title )
    {
        self.pushInfo.alertTitle = title;
    }
    
    self.pushInfo.fireTimeInterval = delay;
    
    self.pushInfo.soundName = sound;
    
    
    //通知类型
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:self.pushInfo.notificationType categories:nil];

    //0.注册通知类型
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
    
    UILocalNotification *local = [[UILocalNotification alloc] init];
    
    local.alertBody = self.pushInfo.alertBody;
    local.alertAction = self.pushInfo.alertAction;
    local.alertTitle =  self.pushInfo.alertTitle;
    local.hasAction =  self.pushInfo.hasAction;
    local.fireDate = [NSDate dateWithTimeIntervalSinceNow:self.pushInfo.fireTimeInterval];
    local.soundName = self.pushInfo.soundName;
    local.alertLaunchImage = self.pushInfo.alertLaunchImage;
    local.userInfo = self.pushInfo.userInfo;
    
        local.applicationIconBadgeNumber = [UIApplication sharedApplication].scheduledLocalNotifications.count + 1;
    
    [[UIApplication sharedApplication] scheduleLocalNotification:local];
    

    
    
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self getPushInfo:delay]];
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
}

- (NSDictionary *)getPushInfo:(NSTimeInterval)delay
{
    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    
    //推送时间
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:delay];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *dateStr = [formatter stringFromDate:date];
    
    [options setObject:dateStr forKey:@"times"];
    [options setObject:self.pushInfo.alertTitle forKey:@"title"];
    [options setObject:self.pushInfo.alertBody forKey:@"content"];
    
    return options;
}
//获取时间
//- (NSTimeInterval)comparedWithCurrentTime:(NSString *)dateStr
//{
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//    
//    NSDate *date = [formatter dateFromString:dateStr];
//    NSTimeInterval timeInterval =  [date timeIntervalSinceNow];
//    return timeInterval;
//}


- (void)clear:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    CDVPluginResult *result = nil;
    
    if ([UIApplication sharedApplication].scheduledLocalNotifications.count)
    {
       result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@"清空所有推送消息成功！"];
    }
    else
    {
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"没有任何消息"];
    }
    
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
}

- (void)getClientInfo:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    NSMutableDictionary *clientInfo = [NSMutableDictionary dictionary];
//    [clientInfo setObject:[DataCenter defaultCenter].deviceToken forKey:@"token"];
    NSLog(@"%@",[DataCenter defaultCenter].deviceToken);
    
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:clientInfo];
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (CDVPushInfo *)pushInfo
{
    if (_pushInfo == nil)
    {
        _pushInfo = [[CDVPushInfo alloc] init];
    }
    return _pushInfo;
}

@end
